//
//  QHBasicViewController.m
//  helloworld
//
//  Created by chen on 14/6/30.
//  Copyright (c) 2014年 chen. All rights reserved.
//

#import "QHBasicViewController.h"
#import "MBProgressHUD.h"
#define isIos7      ([[[UIDevice currentDevice] systemVersion] floatValue])

@interface QHBasicViewController ()
{
    float _nSpaceNavY;
    
}
@end

@implementation QHBasicViewController

- (id)initWithFrame:(CGRect)frame param:(NSArray *)arParams
{
    self.arParams = arParams;
    
    self = [super init];
    
    [self.view setFrame:frame];
    
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [[self navigationController] setNavigationBarHidden:YES];
    
    [super viewWillAppear:TRUE];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //self.view.backgroundColor = RGBA(236.f, 236.f, 236.f, 1);
    
    _statusBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 320, 0.f)];
    _nSpaceNavY = 20;
    if (isIos7 >= 7 && __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1)
    {
        _statusBarView.frame = CGRectMake(_statusBarView.frame.origin.x, _statusBarView.frame.origin.y, _statusBarView.frame.size.width, 20.f);
        _statusBarView.backgroundColor = [UIColor clearColor];
        ((UIImageView *)_statusBarView).backgroundColor = [UIColor clearColor];
        [self.view addSubview:_statusBarView];
        //[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
        
        _nSpaceNavY = 0;
    }
}

- (void)createNavWithTitle:(NSString *)szTitle createMenuItem:(UIView *(^)(int nIndex))menuItem
{
//    UIImageView *navIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, _nSpaceNavY, self.view.width, 64 - _nSpaceNavY)];
//    navIV.tag = 98;
//    [self.view addSubview:navIV];
//    [self reloadImage];
    
    /* { 导航条 } */
    //_navView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, StatusbarSize, 320, 44.f)];
    //((UIImageView *)_navView).backgroundColor = [UIColor clearColor];
    //_navView = [[UIView alloc] initWithFrame:CGRectMake(0.f, StatusbarSize, 320, 44.f)];
    _navView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 320, 20.f+44.f)];
    _navView.backgroundColor=RGBColor(40, 119, 193, 1);
    [self.view addSubview:_navView];
    _navView.userInteractionEnabled = YES;
    
    UILabel *titleLabel;
    if (szTitle != nil)
    {
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((_navView.width - 200)/2, 20.0, 200, 44)];
        [titleLabel setText:szTitle];
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        self.titleLab=titleLabel;
        [_navView addSubview:titleLabel];
    }
    
    //左按钮
    UIView *item1 = menuItem(0);
    if (item1 != nil)
    {
        _leftV = item1;
        [_navView addSubview:item1];
    }
    //中间搜索
    UIView *item0 = menuItem(1);
    if (item0 != nil)
    {
        _centerV = item0;
        [_navView addSubview:item0];
    }
    //右按钮
    UIView *item2 = menuItem(2);
    if (item2 != nil)
    {
        _rightV = item2;
        [_navView addSubview:item2];
    }
    UIView *item3 = menuItem(3);
    if (item3 != nil)
    {
        //_rightV = item2;
        [_navView addSubview:item3];
    }
    UIView *item4 = menuItem(4);
    if (item4 != nil)
    {
        //_rightV = item2;
        [_navView addSubview:item4];
    }
}
- (void)createNavWithTitleSub:(NSString *)szTitle subtitle:(NSString*)subTitle createMenuItem:(UIView *(^)(int nIndex))menuItem {
    _navView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 320, 20.f+44.f)];
    _navView.backgroundColor=RGBColor(40, 119, 193, 1);
    [self.view addSubview:_navView];
    _navView.userInteractionEnabled = YES;
    
    UILabel *titleLabel;
    if (szTitle != nil)
    {
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((_navView.width - 200)/2, 20.0, 200, 20)];
        [titleLabel setText:szTitle];
        [titleLabel setTextAlignment:NSTextAlignmentCenter];
        [titleLabel setTextColor:[UIColor whiteColor]];
        [titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
        [titleLabel setBackgroundColor:[UIColor clearColor]];
        self.titleLab=titleLabel;
        [_navView addSubview:titleLabel];
    }
    UILabel *subLabel;
    if (subTitle != nil)
    {
        subLabel = [[UILabel alloc] initWithFrame:CGRectMake((_navView.width - 200)/2, titleLabel.bottom, 200, 20)];
        [subLabel setText:subTitle];
        [subLabel setTextAlignment:NSTextAlignmentCenter];
        [subLabel setTextColor:[UIColor whiteColor]];
        [subLabel setFont:[UIFont boldSystemFontOfSize:12]];
        [subLabel setBackgroundColor:[UIColor clearColor]];
        //self.titleLab=subLabel;
        [_navView addSubview:subLabel];
    }
    
    //左按钮
    UIView *item1 = menuItem(0);
    if (item1 != nil)
    {
        _leftV = item1;
        [_navView addSubview:item1];
    }
    //中间搜索
    UIView *item0 = menuItem(1);
    if (item0 != nil)
    {
        _centerV = item0;
        [_navView addSubview:item0];
    }
    //右按钮
    UIView *item2 = menuItem(2);
    if (item2 != nil)
    {
        _rightV = item2;
        [_navView addSubview:item2];
    }
}
- (void)addObserver
{
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateProgress:) name:MUSICEPRO object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(observerReloadImage:) name:RELOADIMAGE object:nil];
    
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateMusicInfo:) name:MUSICINFO object:nil];
}


- (void)reloadImage
{
    /*
    NSString *imageName = nil;
    if (isIos7 >= 7 && __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1)
    {
        imageName = @"header_bg_ios7.png";
    }else
    {
        imageName = @"header_bg.png";
    }
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *navIV = (UIImageView *)[self.view viewWithTag:98];
    [navIV setImage:image];
    */
}

- (void)observerReloadImage:(NSNotificationCenter *)notif
{
    [self reloadImage:notif];
}

- (void)reloadImage:(NSNotificationCenter *)notif
{
    [self reloadImage];
}

- (void)subReloadImage
{
    NSLog(@"subReloadImage");
}

//公共
-(void)showAlertWithTitle:(NSString *)t andContent:(NSString *)c{
    [self showAlertWithTitle:t andContent:c cancelable:YES];
}

-(void)showAlertWithContent:(NSString *)c{
    [self showAlertWithTitle:nil andContent:c cancelable:YES];
}

-(void)showAlertWithTitle:(NSString *)t andContent:(NSString *)c cancelable:(BOOL)b{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:t message:c delegate:self cancelButtonTitle:b?@"确定":nil otherButtonTitles:
                          nil, nil];
    if (!b) {
        alert.cancelButtonIndex = -1;
    }
    [alert show];
}

-(void)showHudWithText:(NSString *)text justText:(BOOL)b autoHide:(BOOL)a{
    [MBProgressHUD hideHUDForView:self.view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = text;
    if (b) {
        hud.mode = MBProgressHUDModeText;
    }else{
        hud.mode = MBProgressHUDModeIndeterminate;
    }
    if (a) {
        [hud showWhileExecuting:@selector(waitForTwoSeconds)
                       onTarget:self withObject:nil animated:YES];
    }
}
- (void)waitForTwoSeconds {
    sleep(2);
}
-(void)showDoneHudWithText:(NSString *)text autoHide:(BOOL)a{
    [MBProgressHUD hideHUDForView:self.view animated:NO];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.customView = [[UIImageView alloc] initWithImage:
                      [UIImage imageNamed:@"checkmark.png"]];
    hud.mode = MBProgressHUDModeCustomView;
    hud.labelText = text;
    if (a) {
        [hud showWhileExecuting:@selector(waitForTwoSeconds)
                       onTarget:self withObject:nil animated:YES];
    }
}

-(void)hideHud{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
@end
